package com.hzhr.iot.statis.sdk.service;

import com.hzhr.iot.core.constant.Common_ErrorsEnum;
import com.hzhr.iot.core.dto.ResponseExceptionDto;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrHttpUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.statis.sdk.constant.Statis_ErrorsEnum;
import com.hzhr.iot.statis.sdk.model.deviceMonthlyGasConsume.dataSum.DeviceMonthlyGasConsumeDataSumModel;
import com.hzhr.iot.statis.sdk.model.deviceMonthlyGasConsume.DeviceMonthlyGasConsumeGetModel;
import com.hzhr.iot.statis.sdk.model.deviceMonthlyGasConsume.DeviceMonthlyGasConsumeModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Service
public class Statis_DeviceMonthlyGasConsumeService {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 查询登录用户管辖范围内的设备的月用气量情况 <br/><br/>
     * 简介&nbsp;:&nbsp;查询登录用户管辖范围内的设备的月用气量情况 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询登录用户的所有管辖部门：(√) <br/>
     * &nbsp;使用GLOBAL方式调用部门服务获取用户管辖范围内的所有部门模型对象deptModelList_1， <br/>
     * &nbsp;条件参数如下： <br/>
     * &nbsp;_like_resourceCode&nbsp;=&nbsp;登录用户.userCode&nbsp;+&nbsp;'%'； <br/>
     * &nbsp;然后提取deptModelList_1元素的id属性，得到id属性集合deptIdlist_1； <br/>
     * 2.&nbsp;查询设备的月用气量：(√) <br/>
     * &nbsp;根据条件参数查询设备的月用气量(DEVICE_MONTHLY_GAS_CONSUME)对象集合deviceMonthLyGasConsumeList_1及总数， <br/>
     * &nbsp;额外筛选规则如下： <br/>
     * &nbsp;deviceMonthLyGasConsumeList_1[i].deptId&nbsp;in&nbsp;deptIdlist_1； <br/>
     * 3.&nbsp;返回信息：(√) <br/>
     * &nbsp;根据条件参数和查询到的deptMonthLyGasConsumeList_1及总数生成结果对象并返回； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _is_paging : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否分页;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_collectDeviceId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where deviceMonthlyGasConsume.collectDeviceId = #{_equal_collectDeviceId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_iotDeviceId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where deviceMonthlyGasConsume.iotDeviceId = #{_equal_iotDeviceId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_gasUserId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where deviceMonthlyGasConsume.gasUserId = #{_equal_gasUserId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_deptId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where deviceMonthlyGasConsume.deptId = #{_equal_deptId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_companyId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where deviceMonthlyGasConsume.companyId = #{_equal_companyId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _greaterthan_date : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where deviceMonthlyGasConsume.date > #{_greaterthan_date};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _greaterthanorequal_date : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where deviceMonthlyGasConsume.date >= #{_greaterthanorequal_date};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _lessthan_date : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where deviceMonthlyGasConsume.date < #{_lessthan_date};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _lessthanorequal_date : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where deviceMonthlyGasConsume.date <= #{_lessthanorequal_date};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _orderBy_date : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 排序 : order by deviceMonthlyGasConsume.date #{_orderBy_date};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/><br/>
     * @throws ServiceException <br/>
     * @see DeviceMonthlyGasConsumeGetModel result <br/>
     */ 
    public DeviceMonthlyGasConsumeGetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(null, headers);

        Map<String, Object> uriVariables = new HashMap<>();

        try {
            ResponseEntity<DeviceMonthlyGasConsumeGetModel> result = restTemplate.exchange("http://statis-service/device-monthly-gas-consumes?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.GET, httpEntity, DeviceMonthlyGasConsumeGetModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(Statis_ErrorsEnum.getEnumByName("DEVICEMONTHLYGASCONSUMESERVICE_GET_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(Statis_ErrorsEnum.getEnumByName("DEVICEMONTHLYGASCONSUMESERVICE_GET_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
    /**
     * 查询设备的月用气量的数据总和 <br/><br/>
     * 简介&nbsp;:&nbsp;查询设备的月用气量的数据总和 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询设备的月用气量总和：(√) <br/>
     * 根据条件参数查询DeviceMonthlyGasConsumeModel对象集合的data属性值的总和sum_1； <br/>
     * 2.&nbsp;返回信息：(√) <br/>
     * 创建结果对象result，并设置属性result.dataSum&nbsp;=&nbsp;sum_1； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _equal_collectDeviceCategoryCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceMonthlyGasConsumeModel.collectDeviceCategoryCode = #{_equal_collectDeviceCategoryCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _like_collectDeviceResourceCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceMonthlyGasConsumeModel.collectDeviceResourceCode like #{_like_collectDeviceResourceCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _greaterthanorequal_collectDeviceInstallTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceMonthlyGasConsumeModel.collectDeviceInstallTime >= #{_greaterthanorequal_collectDeviceInstallTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _lessthan_collectDeviceInstallTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceMonthlyGasConsumeModel.collectDeviceInstallTime < #{_lessthan_collectDeviceInstallTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _greaterthan_date : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceMonthlyGasConsumeModel.date > #{_greaterthan_date};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _greaterthanorequal_date : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceMonthlyGasConsumeModel.date >= #{_greaterthanorequal_date};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _lessthan_date : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceMonthlyGasConsumeModel.date < #{_lessthan_date};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _lessthan_data : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceMonthlyGasConsumeModel.data < #{_lessthan_data};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _notin_gasUserCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DEVICE_MONTHLY_GAS_CONSUME.DEVICE_COLLECT_DEVICE(关联：COLLECT_DEVICE_ID) not in #{_in_gasUserCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : list;<br/><br/>
     * @throws ServiceException <br/>
     * @see DeviceMonthlyGasConsumeDataSumModel result <br/>
     */ 
    public DeviceMonthlyGasConsumeDataSumModel getDataSum(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(null, headers);

        Map<String, Object> uriVariables = new HashMap<>();

        try {
            ResponseEntity<DeviceMonthlyGasConsumeDataSumModel> result = restTemplate.exchange("http://statis-service/device-monthly-gas-consumes/data-sum?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.GET, httpEntity, DeviceMonthlyGasConsumeDataSumModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(Statis_ErrorsEnum.getEnumByName("DEVICEMONTHLYGASCONSUMESERVICE_GET_DATA_SUM_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(Statis_ErrorsEnum.getEnumByName("DEVICEMONTHLYGASCONSUMESERVICE_GET_DATA_SUM_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
}

